package com.PlantVsZombies.online;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.PlantVsZombies.ui.PlantsMap;


public class ServerThread implements Runnable{

	private PlantsMap plantsmap;
	
	private Integer port;
	
	private Thread t;
	
	public ServerThread(PlantsMap plantsmap, Integer port, Thread t) {
		super();
		this.plantsmap = plantsmap;
		this.port = port;
		this.t = t;
	}

	@Override
	public void run() {
		try {
			ServerSocket serverSocket = new ServerSocket(port);
			while(true) {
				Socket socket = serverSocket.accept();
				System.out.println("连接成功");
				if (!t.isAlive()) {
					t.start();
				}
				DataTransfer transfer = new DataTransfer(socket, t);
				if (plantsmap.getTransfer() == null) {
					plantsmap.setTransfer(transfer);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
